package com.itany.springcloud.springcloudeurekaprovider8003.controller;

import com.itany.springcloud.springcloudcommon.entity.User;
import com.itany.springcloud.springcloudcommon.vo.ResponseResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private static Map<Integer, User> users=new HashMap<Integer,User>();
    private static Integer id=1;


    @PostMapping
    public ResponseResult add(User user){
        System.out.println("provider.UserController.add()");

        user.setId(id++);
        users.put(user.getId(),user);

        return ResponseResult.success();
    }

    @PostMapping("/add2")
    public ResponseResult add2(@RequestBody User user){
        System.out.println("provider.UserController.add2()");

        user.setId(id++);
        users.put(user.getId(),user);

        return ResponseResult.success();
    }

    @PostMapping("/add3")
    public ResponseResult add3(User user){
        System.out.println("provider.UserController.add3()");

        user.setId(id++);
        users.put(user.getId(),user);

        return ResponseResult.success();
    }


    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("provider.UserController.remove()");

        users.remove(id);

        return ResponseResult.success();
    }

    @PutMapping
    public ResponseResult modify(User user){
        System.out.println("provider.UserController.modify()");

        users.put(user.getId(),user);

        return ResponseResult.success();
    }

    @GetMapping
    public ResponseResult findAll(){
        System.out.println("provider.UserController.findAll()");

        List<User> list=new ArrayList<User>(users.values());

        return ResponseResult.success("OK-8003",list);
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("provider.UserController.findById()");

        User user=users.get(id);

        return ResponseResult.success(user);
    }

}
